package com.sgdh.quote.server.handler;

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import javax.imageio.ImageIO;

import org.springframework.stereotype.Component;

import com.sgdh.quote.common.CaptureImage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInboundHandlerAdapter;


@ChannelHandler.Sharable
@Component
public class ServerHandler extends ChannelInboundHandlerAdapter {
	private BufferedImage image;
	/**
	 *robot类控制鼠标及肩旁操作
	 */
	private Robot robot;

	/**
	 *获取显示尺寸和分辨率以及获得系统属性信息
	 */
	private Toolkit toolkit;

	/**
	 *表示一个矩形
	 */
	private Rectangle rect;

	/**
	 *字节数组输出流
	 */
	private ByteArrayOutputStream baos;

	/**
	 *图片对象
	 */
	private CaptureImage captureImage;

	public ServerHandler() {
		try {
			//初始化Robot控制对象
			robot = new Robot();
			//获取本地窗口对象
			toolkit = Toolkit.getDefaultToolkit();
			//根据窗口对象初始化矩形大小
			rect = new Rectangle(0, 0, toolkit.getScreenSize().width, toolkit.getScreenSize().height);
			//初始化字节数据输出流
			baos = new ByteArrayOutputStream();
			//构建传输对象
			captureImage = new CaptureImage();
		} catch (AWTException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {

	}

	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {

	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		try {
			String message = (String) msg;
			String title = message.substring(0,20).trim();
			int panelId = Integer.parseInt(message.substring(20,40).trim());
			//根据消息类型做对应处理
			switch (title) {
			case "ACK":
				image = robot.createScreenCapture(rect);
				ImageIO.write(image, "jpg", baos);
				captureImage.setLength(baos.toByteArray().length);
				captureImage.setContent(baos.toByteArray());
				captureImage.setPanelId(panelId);
				ctx.writeAndFlush(captureImage);
				baos.reset();
				break;
			case "moveTo":
				int x = Integer.valueOf(message.substring(40, 50).trim());
				int y = Integer.valueOf(message.substring(50).trim());
				robot.mouseMove(x, y);
				break;
			case "leftClick":
				robot.mousePress(InputEvent.BUTTON1_MASK);
				robot.mouseRelease(InputEvent.BUTTON1_MASK);
				break;
			case "rightClick":
				robot.mousePress(InputEvent.BUTTON3_MASK);
				robot.mouseRelease(InputEvent.BUTTON3_MASK);
				break;
			case "leftDown":
				robot.mousePress(InputEvent.BUTTON1_MASK);
				break;
			case "leftUp":
				robot.mouseRelease(InputEvent.BUTTON1_MASK);
				break;
			case "wheel":
				int wheelAmt = Integer.valueOf(message.substring(40, 50).trim());
				robot.mouseWheel(wheelAmt);
				break;
			case "keyType":
				int keycode = Integer.valueOf(message.substring(40, 50).trim());
				robot.keyPress(keycode);
				robot.keyRelease(keycode);
				break;
			case "keyDown":
				int keycode1 = Integer.valueOf(message.substring(40, 50).trim());
				robot.keyPress(keycode1);
				break;
			case "keyUp":
				int keycode2 = Integer.valueOf(message.substring(40, 50).trim());
				robot.keyRelease(keycode2);
				break;
			default:
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {

	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
		System.err.println("An existing connection was forcibly closed by the remote host");
	}
}
